package com.zenithmind.coding.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.coding.controller.base.BaseController;
import com.zenithmind.coding.pojo.dto.ProblemCreateDTO;
import com.zenithmind.coding.pojo.dto.StatusUpdateRequest;
import com.zenithmind.coding.pojo.vo.ProblemVO;
import com.zenithmind.coding.service.ProblemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编程问题控制器 - 遵循单一职责原则，专门处理问题的基本CRUD操作
 *
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "编程问题", description = "编程问题管理相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/problems")
@RequiredArgsConstructor
public class ProblemController extends BaseController {

    private final ProblemService problemService;

    @Operation(summary = "创建问题", description = "创建新的编程问题")
    @PostMapping
    public Result<ProblemVO> createProblem(@Valid @RequestBody ProblemCreateDTO createDTO) {
        try {
            validateUserRole("TEACHER");
            ProblemVO problem = problemService.createProblem(createDTO);
            logOperation("创建问题", "问题标题: " + createDTO.getTitle());
            return success(problem);
        } catch (IllegalAccessException e) {
            logError("创建问题", "权限不足", e);
            return Result.fail("权限不足");
        }
    }

    @Operation(summary = "分页查询问题列表", description = "分页查询编程问题列表")
    @GetMapping
    public Result<PageResult<ProblemVO>> getProblemPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId,
            @Parameter(description = "难度等级") @RequestParam(required = false) Integer difficulty,
            @Parameter(description = "问题类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "问题状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "标签列表") @RequestParam(required = false) List<String> tags) {
        validatePageParams(current, size);
        PageResult<ProblemVO> result = problemService.getProblemPage(current, size, categoryId,
                                                                    difficulty, type, status, keyword, tags);
        return success(result);
    }

    @Operation(summary = "查询问题详情", description = "根据ID查询编程问题详情")
    @GetMapping("/{id}")
    public Result<ProblemVO> getProblemById(@Parameter(description = "问题ID") @PathVariable String id) {
        validateId(id, "问题ID");
        String userId = getCurrentUserId();
        ProblemVO problem = problemService.getProblemById(id, userId);
        logOperation("查询问题详情", "问题ID: " + id);
        return success(problem);
    }

    @Operation(summary = "更新问题", description = "更新编程问题信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateProblem(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Valid @RequestBody ProblemCreateDTO updateDTO) {
        Boolean result = problemService.updateProblem(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除问题", description = "删除指定编程问题")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        Boolean result = problemService.deleteProblem(id);
        return Result.success(result);
    }

    @Operation(summary = "发布问题", description = "发布指定编程问题")
    @PostMapping("/{id}/publish")
    public Result<Boolean> publishProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        Boolean result = problemService.publishProblem(id);
        return Result.success(result);
    }

    @Operation(summary = "下架问题", description = "下架指定编程问题")
    @PostMapping("/{id}/unpublish")
    public Result<Boolean> unpublishProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        Boolean result = problemService.unpublishProblem(id);
        return Result.success(result);
    }

    @Operation(summary = "更新问题状态", description = "更新指定编程问题的状态")
    @PutMapping("/{id}/status")
    public Result<Boolean> updateProblemStatus(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Parameter(description = "状态") @RequestBody StatusUpdateRequest request) {
        Boolean result = problemService.updateProblemStatus(id, request.getStatus());
        return Result.success(result);
    }

    @Operation(summary = "审核问题", description = "审核指定编程问题")
    @PostMapping("/{id}/review")
    public Result<Boolean> reviewProblem(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Parameter(description = "是否通过") @RequestParam Boolean approved,
            @Parameter(description = "审核意见") @RequestParam(required = false) String comment) {
        Boolean result = problemService.reviewProblem(id, approved, comment);
        return Result.success(result);
    }





    @Operation(summary = "批量导入问题", description = "批量导入编程问题")
    @PostMapping("/batch/import")
    public Result<Integer> batchImportProblems(@Valid @RequestBody List<ProblemCreateDTO> problems) {
        Integer count = problemService.batchImportProblems(problems);
        return Result.success(count);
    }

    @Operation(summary = "导出问题", description = "导出指定编程问题")
    @PostMapping("/batch/export")
    public Result<List<ProblemVO>> exportProblems(@RequestBody List<String> problemIds) {
        List<ProblemVO> problems = problemService.exportProblems(problemIds);
        return Result.success(problems);
    }
}
